package com.videoanswer.van.web.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.videoanswer.van.persistence.domain.user.User;
import com.videoanswer.van.service.user.UserService;
import com.videoanswer.van.service.user.model.UserModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class RegisterController {

    private UserService userService;
	
	@RequestMapping(value = { "/register" }, method = { RequestMethod.GET })
	public ModelAndView getRegister(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("hasMianNav", false);
		
		return new ModelAndView("/register", map);
	}
	
	@RequestMapping(value = { "/register" }, method = { RequestMethod.POST })
	public ModelAndView postRegister(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("hasMianNav", false);

        UserModel userModel = new UserModel();
		boolean registerSucceed = true;
		
		userModel.setName(request.getParameter("username"));
        userModel.setEmail(request.getParameter("email"));
        userModel.setPassword(request.getParameter("password"));
        userModel.setMobilePhone(request.getParameter("cellphone"));
		
		try{
            userModel = userService.register(userModel);
		}catch(Exception e){
			System.out.println(e.toString());
			registerSucceed = false;
		}
		
		return new ModelAndView("/register", map);
	}

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

